package com.itheima.myutil.localdateutil;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class _myLocalDateTime {
    public _myLocalDateTime() {
    }
    // 按照默认的模式“yyyy-MM-dd HH:mm:ss”将日期对象格式化为字符串
    public static String localDateTimeToString(LocalDateTime dateTime){
        return localDateTimeToString(dateTime,null);
    }
    // 按指定的模式将日期对象格式化为字符串
    public static String localDateTimeToString(LocalDateTime dateTime,String pattern){
        if(pattern==null||"".equals(pattern)){
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String s = formatter.format(dateTime);
        return s;
    }
    // 按照指定的模式将字符串解析为日期对象
    public static LocalDateTime stringToLocalDateTime(String dateStr,String pattern){
        if(pattern==null||"".equals(pattern)){
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime parse = LocalDateTime.parse(dateStr);
        return parse;
    }
    // 按照默认的模式“yyyy-MM-dd HH:mm:ss”将字符串解析为日期对象
    public static LocalDateTime stringToLocalDateTime(String dateStr) {
        return stringToLocalDateTime(dateStr,null);
    }
        // 两个日期对象的时间间隔
    public static long millisSecondBetween(LocalDateTime dateTime1,LocalDateTime dateTime2){
        return Duration.between(dateTime1,dateTime2).abs().toMillis();
    }
}
